#!/bin/bash

# 安装
function installToHammerspoonPlugins()
{
    pluginsName=$1
    echo "[Debug] pluginsName="${pluginsName}

    # 拆分字符串 
    # 原理：
    # ${parameter//pattern/string} 
    # 用string来替换parameter变量中所有匹配的pattern
    # 例子：以 , 拆分字符串
    #  string="hello,shell,split,test" 
    #  array=(${string//,/ }) 
    #  echo "[Debug] 循环输出 array[]所有值="${array[1]}
    # for var in ${array[@]}
    # do
    #    echo $var
    # done
    #  参考 [Shell_Linux Shell 中实现字符串切割的几种方法](https://blog.csdn.net/u010003835/article/details/80750003)

    # 以/ 拆分字符串
    array=(${pluginsName//\// })
    # echo "[Debug] 循环输出 array[]所有值="${array[1]}
    # for var in ${array[@]}
    # do
    #   echo $var
    # done
    # echo "[Debug] array[1]="${array[1]}
    pluginsPath=${HOME}"/.hammerspoon/"${array[1]}
    #pluginsPath=${ZSH_CUSTOM}"/plugins/"${pluginsName}
    echo "[Debug] pluginsPath="${pluginsPath}

    echo "[INFO] 安装插件："${pluginsName}
    # 判断是否已经存在
    if [ ! -d "$pluginsPath" ]; then
      echo "Installing "${pluginsName}
      #URL="https://github.com/"
      #URL="https://hub.fastgit.org/"
      # URL="https://ghproxy.com/https://github.com/"
      URL="https://github.com/"
      if [ -x "$(which git)" ]; then
        # 命令拼接
        cmdStr="git clone "${URL}${pluginsName}" "${pluginsPath}
        echo "执行命令: ${cmdStr}"
        ${cmdStr}

        echo "[INFO] 安装插件"${pluginsName}"安装完成！"

      else
        echo "[ERROR]: please install git  before installation !!"
        exit
      fi
      if [ ! $? -eq 0 ]; then
        echo ""
        echo "ERROR: downloading ${pluginsName}  failed !!"
        exit
      fi
    else
        echo "[INFO] 插件${pluginsName}已经存在！"
    fi
}
#    / **  
#    *〈 判断文件夹是否存在，不存在则，建立链接，存在则备份并建立链接 〉  
#    *〈功能详细描述〉  
#    * @param [参数1] [文件路径]  
#    * @param [参数2] [链接建立命令]  
#    * @return [返回类型说明]  
#    * @exception/throws [违例类型] [违例说明]  
#    * @see [类、类#方法、类#成员]  
#    * @deprecated  
#    * 例子：
#    *      判断文件夹 ~/.vim是否存在，不存在则执行命令，存在则备份后执行命令 
#    *      folderPath=${HOME}"/.vim"
#    *      echo "[Debug] folderPath= ${folderPath}"
#    *      commandStr="mkdir -p ${folderPath}"
#    *      echo "[Debug] commandStr=${commandStr}"
#    *      bakAndLinkFolder ${folderPath} "${commandStr}"
#    *      # 注：参数中带有空格，如果直接写的话，会从空格处自动切分开。解决方法：将这个引用变量用双引号括起来
#    */  
function bakAndMakeFolderLink()
{
    echo "[Debug]: bakAndLinkFolder !";
    # folderPath=${HOME}"/.vim"
    originFolderPath=$1;
    targetFolderPath=$2;
    commandStr=$3;
    echo "[INFO]: originFolderPath=${originFolderPath}";
    echo "[INFO]: targetFolderPath=${targetFolderPath}";
    echo "[INFO]: commandStr=${commandStr}";

    # 判断是文件夹/文件夹软链接是否已经存在
    if [[ "$(readlink ${targetFolderPath})" = "${originFolderPath}" ]]; then
       #echo "[INFO]: 文件夹软链接已经存在 ";
       success "${targetFolderPath} ==>${originFolderPath} 文件夹软链接已经存在"
    else
        if [ -L "$targetFolderPath" ] || [ -d "$targetFolderPath" ] ; then
            echo "[INFO] 备份文件夹/文件夹软链接 ${targetFolderPath}";
            cmdStr="mv ${targetFolderPath} ${targetFolderPath}-`date +%y%m%d%H%M%S`";
            echo "执行命令: ${cmdStr}";
            ${cmdStr}
        fi
        echo "[INFO]: 创建文件夹软链接";
        cmdStr="ln -s  ${originFolderPath}  ${targetFolderPath}"
        echo "执行命令: ${cmdStr}";
        ${cmdStr}

        # 执行命令
        echo "执行命令: ${commandStr}";
        ${commandStr}
    fi
}


#    / **  
#    *〈流程控制〉  
#    *〈该脚本的主函数〉  
#    * @param [无]  
#    * @return [无]  
#    * @exception/throws [无]  
#    * @deprecated  
#    */  
function processControlMain()  
{  
    # 打印当前系统时间  
    echo "Start..."  
    date  

    # 此处是你的代码  
    # ...  
    ## font


    # Make link
    ## 获取当前脚本目录位置
        # 获取当前脚本目录位置
        dir=`pwd`
        echo "获取当前脚本目录位置:"${dir}
    #获取仓库
    #installToHammerspoonPlugins "AdamWagner/stackline"
    ## 配置文件
    #当hammerspoon 启动时让stackline 运行
    #cd  ~/.hammerspoon
    #echo  ' stackline = require "stackline" '  >> init.lua
    #echo  ' stackline:init() '  >> init.lua
    
    #
    bakAndMakeFolderLink "${dir}/" "${HOME}/.hammerspoon"
    # 打印当前系统时间  
    date  
    echo "The End."  
}  

# 执行入口 
# 进入脚本目录位置
#cd `dirname $0`
# 获取当前脚本目录位置
shellfilepath=`pwd`'/'$0
# 打印脚本目录地址
echo "[INFO]: 脚本地址 "${shellfilepath}

if true  
# if false  
then  
    echo "[Debug] true"
    # 开始执行  从标准输入中读取并同时写入到标准输出和指定的文件上 
    ## 拼接日志文件地址
    #logfilepath=`pwd`'/'$0'.log'
    #logfilepath=`pwd`'/'$0'-'$(date +%Y%m%d)'.log'
    logfilepath=$0'-'$(date +%Y%m%d)'.log'
    #logfilepath=`pwd`'/'$0'-'$(date +%y%m%d-%H-%M-%S)'.log'
    ## 打印日志文件地址
    echo "[INFO]: 日志地址" ${logfilepath}
    processControlMain  | tee -a ${logfilepath}
else  
    echo "[Debug] false"  
    ## 函数单元测试  
    #  
fi  

# 脚本结束  

